package com.itheima.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.vo.CommentVo;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserManager {

    @DubboReference
    UserInfoService userInfoService;

    @DubboReference
    MovementService movementService;

    @DubboReference
    CommentService commentService;

    public ResponseEntity findByPage(Integer pageNum, Integer pageSize) {
        PageBeanVo pageBeanVo = userInfoService.findByPage(pageNum, pageSize);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findUserInfoById(Long userId) {
        UserInfo userInfo = userInfoService.findById(userId);
        return ResponseEntity.ok(userInfo);
    }

    public ResponseEntity findMovementByCondition4Page(Long userId, Integer state, Integer pageNum, Integer pageSize) {
        //1.判断uid是否为空,若不为空查询userInfo
        UserInfo userInfo = null;
        if (userId!=null) {
            userInfo = userInfoService.findById(userId);
        }

        //2.远程调用service查询pageBeanvo
        PageBeanVo pageBeanVo = movementService.findMovementByCondition4Page(userId, state, pageNum, pageSize);

        //3.获取pageBeanVo中动态集合
        List<Movement> items = (List<Movement>) pageBeanVo.getItems();

        //4.遍历动态集合,获取每个动态,若uid为空是查询动态的发布人userInfo,将动态和userInfo封装成MovementVo对象,将vo放入声明好的voList中
        List<MovementVo> voList = new ArrayList<>();
        if (CollUtil.isNotEmpty(items)) {
            for (Movement movement : items) {
                //若uid为空是查询动态的发布人userInfo
                if (userId == null) {
                    userInfo = userInfoService.findById(movement.getUserId());
                }

                //将动态和userInfo封装成MovementVo对象
                MovementVo vo = new MovementVo();

                //先userInfo后movement
                vo.setUserInfo(userInfo);
                vo.setMovement(movement);

                //单独处理下日期
                vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));

                voList.add(vo);
            }
        }

        //5.将voList设置给pageBeanVo
        pageBeanVo.setItems(voList);
        //6.返回pageBeanVo即可
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findMovementById(String movementId) {
        //1.远程调用service查询动态详情
        Movement movement = movementService.findById(new ObjectId(movementId));

        //2.远程调用service查询动态发布人userInfo
        UserInfo userInfo = userInfoService.findById(movement.getUserId());

        //3.封装MovementVo对象且返回
        MovementVo vo = new MovementVo();
        //先userInfo后movement
        vo.setUserInfo(userInfo);
        vo.setMovement(movement);

        //单独处理下时间
        vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));

        return ResponseEntity.ok(vo);
    }

    public ResponseEntity findMovementComemntByPage(String movementId, Integer pageNum, Integer pageSize) {
        //1.远程调用commentService查询pageBeanVo
        PageBeanVo pageBeanVo = commentService.findCommentByPage(new ObjectId(movementId), pageNum, pageSize);

        //2.获取pageBeanVo中评论list
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();

        //3.遍历评论list,获取每个评论,获取评论人的userInfo,将评论和userInfo封装CommentVo对象,将每个vo放入声明好的voList中
        List<CommentVo> voList = new ArrayList<>();
        if (CollUtil.isNotEmpty(items)) {
            items.forEach(comment -> {
                //获取评论人的userInfo,
                UserInfo userInfo = userInfoService.findById(comment.getUserId());

                //将评论和userInfo封装CommentVo对象,
                CommentVo vo = new CommentVo();
                vo.setId(comment.getId().toString());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());
                vo.setContent(comment.getContent());
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated())));

                //将每个vo放入声明好的voList中
                voList.add(vo);
            });
        }

        //4.将voList设置给pageBeanVo
        pageBeanVo.setItems(voList);
        //5.返回pageBeanVo
        return ResponseEntity.ok(pageBeanVo);
    }
}
